<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/20 0020
 * Time: 11:48
 */

namespace app\sisome\controller;


use think\Db;
use think\Request;

class Trade extends Common
{
    /**
     * @name 求购订单
     * @author zay
     * @Date 2020/3/20 0020 11:53
     * @return array
     */
    public function qiugou(Request $request)
    {
        $start = $request->param('start', 0);
        $end = $request->param('end', 0);
        $uname = $request->param('uname', '');
        $order_sn = $request->param('order_sn', '');
        $list = Db('ppdd')->where(['is_del' => 0]);
        if ($start && $end) {
            $list->whereTime('time', '>=', $start);
            $list->whereTime('time', '<=', $end);
        }
        if ($uname) {
            $list->where(['p_user' => $uname]);
        }
        if ($order_sn) {
            $list->where(['p_order_sn' => $order_sn]);
        }
        $list = $list->order('id desc')->whereIn('zt', [0, 1])->paginate(15, false, ['query' => Request::instance()->param()]);
        $this->assign('lists', $list);
        $this->assign('param', $request->param());
        return view();
    }

    /**
     * @name 交易中的订单
     * @author zay
     * @Date 2020/3/20 0020 11:53
     * @return array
     */
    public function jiaoyi(Request $request)
    {
        $start = $request->param('start', 0);
        $end = $request->param('end', 0);
        $uname = $request->param('uname', '');
        $order_sn = $request->param('order_sn', '');

        $list = Db('ppdd_jiaoyi')->alias('g')
            ->field('g.*')
            ->join('ppdd p', 'p.id=g.p_order_id', 'left')
            ->where(['p.is_del' => 0])
            ->whereIn('g.zt', [1, 4, 5]);
        if ($start && $end) {
            $list->whereTime('g.time', '>=', $start);
            $list->whereTime('g.time', '<=', $end);
        }
        if ($uname) {
            $list->where(['p.p_user' => $uname]);
        }
        if ($order_sn) {
            $list->where(['g.g_order_sn' => $order_sn]);
        }
        $list = $list->order('g.time desc')->paginate(15, false, ['query' => Request::instance()->param()]);
        $this->assign('lists', $list);
        $this->assign('param', $request->param());
        return view();
    }

    /**
     * @name 求购完成订单
     * @author zay
     * @Date 2020/3/20 0020 11:53
     * @return array
     */
    public function complete(Request $request)
    {
        $start = $request->param('start', 0);
        $end = $request->param('end', 0);
        $uname = $request->param('uname', '');
        $order_sn = $request->param('order_sn', '');
        $list = Db('ppdd')->where(['zt' => 2, 'is_del' => 0]);
        if ($start && $end) {
            $list->whereTime('time', '>=', $start);
            $list->whereTime('time', '<=', $end);
        }
        if ($uname) {
            $list->where(['p_user' => $uname]);
        }
        if ($order_sn) {
            $list->where(['p_order_sn' => $order_sn]);
        }
        $list = $list->order('id desc')->paginate(15, false, ['query' => Request::instance()->param()]);
        $this->assign('lists', $list);
        return view();
    }

    /**
     * @name 取消订单
     * @author zay000
     * @Date 2020/3/20 0020 11:53
     * @return array
     */
    public function cancel(Request $request)
    {
        $start = $request->param('start', 0);
        $end = $request->param('end', 0);
        $uname = $request->param('uname', '');
        $order_sn = $request->param('order_sn', '');

        $list = Db('ppdd')->where(['is_del' => 0])->where(['zt' => 3]);
        if ($start && $end) {
            $list->whereTime('time', '>=', $start);
            $list->whereTime('time', '<=', $end);
        }

        if ($uname) {
            $list->where(['p_user' => $uname]);
        }
        if ($order_sn) {
            $list->where(['p_order_sn' => $order_sn]);
        }

        $list = $list->order('update_time desc')->paginate(15, false, ['query' => Request::instance()->param()]);
        $this->assign('lists', $list);
        return view();
    }

    /**
     * @name 出售订单
     * @author zay
     * @Date 2020/3/20 0020 11:53
     * @return array
     */
    public function cswc(Request $request)
    {
        $start = $request->param('start', 0);
        $end = $request->param('end', 0);
        $uname = $request->param('uname', '');
        $order_sn = $request->param('order_sn', '');
        $list = Db('ppdd_jiaoyi')->alias('g')
            ->field('g.*')
            ->join('ppdd p', 'p.id=g.p_order_id', 'left')
            ->where(['p.is_del' => 0, 'g.zt' => 1]);
        if ($start && $end) {
            $list->whereTime('g.time', '>=', $start);
            $list->whereTime('g.time', '<=', $end);
        }
        if ($uname) {
            $list->where(['g.sell_user' => $uname]);
        }
        if ($order_sn) {
            $list->where(['g.g_order_sn' => $order_sn]);
        }
        $list = $list->order('g.id desc')->paginate(15, false, ['query' => Request::instance()->param()]);
        $this->assign('lists', $list);
        $this->assign('param', $request->param());
        return view();
    }

    /**
     * @name 未匹配的求购单删除
     * @author zay
     * @Date 2020/4/8 0008 14:31
     */

    public function del(Request $request)
    {
        $id = $request->param('id', 0);
        try {
            $info = Db('ppdd')->where(['id' => $id, 'is_del' => 0])->find();
            if (empty($info)) {
                throw new \Exception('订单不存在或已删除');
            }
            if ($info['zt'] != 0) {
                throw new \Exception('订单已交易，不能删除');
            }
            $res = Db('ppdd')->where(['id' => $info['id']])->update(['is_del' => 1, 'update_time' => time()]);
            if (!$res) {
                throw new \Exception('删除失败');
            }
            $this->success('操作成功');
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * @name 已匹配的订单取消
     * @author zay
     * @Date 2020/5/9 0008 14:31
     */
    public function jiaoyi_cancel(Request $request){
        $id = $request->param('id', 0);
        DB::startTrans();
        $is_success = 0;
        $msg = '';
        try{
            $this->trade_cancel($id);
            Db::commit();
            $is_success = 1;
        }catch (\Exception $e){
            Db::rollback();
            $msg = $e->getMessage();
        }
        if($is_success){
            $this->success('操作成功');
        }else{
            $this->error($msg);
        }
    }
}